package com.tourismhengyang.actions;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.tourismhengyang.beans.PlaneTicket;
import com.tourismhengyang.beans.TrainTicket;
import com.tourismhengyang.utils.JsonObject;
import com.tourismhengyang.utils.MailUtil;

public class MailAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	private HttpSession session;
	private JsonObject json;
	private String to_email;
	private String t_info;
	private String h_info;
	private MailUtil mail = new MailUtil();
	
	public String sendMail() {
		session = ServletActionContext.getRequest().getSession();
		json = new JsonObject();
		TrainTicket tt = (TrainTicket) session.getAttribute("T_TicketInfo");
		PlaneTicket pt = (PlaneTicket) session.getAttribute("P_TicketInfo");
		String T_info = (String) session.getAttribute("t_info");
		String H_info = (String) session.getAttribute("h_info");
		boolean result = false;
		System.out.println(tt+" "+pt+" "+T_info+" "+H_info);
		if(tt != null) {
			result = mail.sendMail(to_email, "温馨提示", getMsg(tt));
		} else if(pt != null){
			result = mail.sendMail(to_email, "温馨提示", getMsg(pt));
		} else if(T_info != null) {
			result = mail.sendMail(to_email, "温馨提示", T_info);
		} else if(H_info != null) {
			result = mail.sendMail(to_email, "温馨提示", H_info);
		}
		if(result){
			json.setResult(1);
		}else{
			json.setResult(0);
		}
		return SUCCESS;
	}
	
	public String getMsg(TrainTicket tt) {
		String result = "";
		String[] d = tt.getTdate().split("-");
		result += d[1]+"月"+d[2]+"日"+tt.getTname()+"次"+tt.getTgo()+tt.getCtime()+"开往"+tt.getTto()+"。";
		return result;
	}
	
	public String getMsg(PlaneTicket pt) {
		String result = "";
		String[] d = pt.getPdate().split("-");
		result += d[1]+"月"+d[2]+"日"+pt.getPname()+"次"+pt.getPgo()+pt.getCtime()+"飞往"+pt.getPto()+"。";
		return result;
	}
	
	public String sendInfo() {
		session = ServletActionContext.getRequest().getSession();
		if(t_info != null) {
			session.setAttribute("t_info", t_info);
		}else if(h_info != null) {
			session.setAttribute("h_info", h_info);
		}
		return SUCCESS;
	}

	public JsonObject getJson() {
		return json;
	}

	public void setJson(JsonObject json) {
		this.json = json;
	}

	public String getTo_email() {
		return to_email;
	}

	public void setTo_email(String to_email) {
		this.to_email = to_email;
	}

	public String getT_info() {
		return t_info;
	}

	public void setT_info(String t_info) {
		this.t_info = t_info;
	}

	public String getH_info() {
		return h_info;
	}

	public void setH_info(String h_info) {
		this.h_info = h_info;
	}
}
